package com.yl.demo.web.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.yl.demo.model.YlUserAddressModel;
import com.yl.demo.utils.BaseUtil;
import com.yl.demo.utils.Result;
import com.yl.demo.web.HelloController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.web.bind.annotation.*;
import org.yl.db_core.base.BaseDao;
import org.yl.db_core.utils.constant.SqlConstant;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YL on 2019/10/17.
 * 地址管理
 */

@RestController
@RequestMapping("/address/*")
public class AddressController {

    private final static Logger log = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    BaseDao baseDao;

    /**
     * 地址列表
     * @return
     */
    @RequestMapping(value = "/addressList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Result queryAddressList(@RequestBody(required = false) YlUserAddressModel addressModel){
        // YlUserAddressModel userAddress = new YlUserAddressModel();
        // addressModel.setIsRemoved("0");
        List list = baseDao.queryByList(addressModel, SqlConstant.SELECT_LIST, new BeanPropertyRowMapper(YlUserAddressModel.class), addressModel.page, addressModel.row);
        Integer total = baseDao.queryByListCount(addressModel, SqlConstant.SELECT_LIST_COUNT, new BeanPropertyRowMapper(YlUserAddressModel.class));
        Result result = new Result(200, "成功");
        Map map = new HashMap();
        map.put("total", total);
        map.put("data", list);
        map.put("page", addressModel.page);
        map.put("row", addressModel.row);
        result.data = map;
        log.info("地址列表返回结果===["+ BaseUtil.toJSON(result)+"]");
        return result;
    }

    /**
     * 查询单个地址信息
     * @return
     */
    @RequestMapping(value = "/addressById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @JsonProperty(value = "IdentityCard")
    public Result queryAddressById(@RequestBody(required = false) YlUserAddressModel addressModel){
        YlUserAddressModel userAddress = (YlUserAddressModel)baseDao.queryById(addressModel, SqlConstant.SELECT_ID);
        Result result = new Result(200, "成功");
        result.data = userAddress;
        log.info("查询单个地址信息===["+ BaseUtil.toJSON(result)+"]");
        return result;
    }



    /**
     * 更新地址信息
     * @return
     */
    @RequestMapping(value = "/addOrUpdateAddress", method = RequestMethod.POST)
    @JsonProperty(value = "IdentityCard")
    public Result addOrUpdateAddress(@RequestBody(required = false) YlUserAddressModel addressModel, HttpServletRequest request){
        Result result = new Result(200, "成功");
        log.info("addressModel==="+addressModel);
        if (addressModel == null){
            result = new Result(205, "参数必须是json格式");
        }else{
            //addressModel.setId((int) System.currentTimeMillis());
            //addressModel.setIsRemoved("0");
        }
        int code = 0;
        log.info("id==="+addressModel.getId());
        if (addressModel.getId() == null) {
            code = baseDao.saveOrUpdateOrDeleteforJdbc(addressModel, SqlConstant.INSERT);
        }else{
            code = baseDao.saveOrUpdateOrDeleteforJdbc(addressModel, SqlConstant.UPDATE);
        }
        if (code > 0){
            result.data = code;
        }else{
            result = new Result(205, "更新地址信息失败");
        }
        log.info("更新地址信息===["+ BaseUtil.toJSON(result)+"]");
        return result;
    }


    /**
     * 删除地址信息
     * @return
     */
    @RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
    @JsonProperty(value = "IdentityCard")
    public Result deleteAddress(@RequestBody(required = false) YlUserAddressModel addressModel, HttpServletRequest request){
        Result result = new Result(200, "成功");
        log.info("addressModel==="+addressModel);
        if (addressModel == null){
            result = new Result(205, "参数必须是json格式");
        }
        int code = baseDao.saveOrUpdateOrDeleteforJdbc(addressModel, SqlConstant.DELETE);;
        if (code > 0){
            result.data = code;
        }else{
            result = new Result(205, "删除失败");
        }
        log.info("删除地址信息===["+ BaseUtil.toJSON(result)+"]");
        return result;
    }

}
